package com.mofang.powerdekorhelper.persenter;

import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.model.ActivityInfo;
import com.mofang.powerdekorhelper.utils.Constants;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.ActivityManagerView;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/8/24 0024.
 */

public class ActivityManagerPersenter extends BasePresent<ActivityManagerView> {

    Call<ActivityInfo> activityInfoCall;
    RetrofitSerives retrofitSerives;

    public void getActivityInfo(Integer activityId) {

        view.showProgress();

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("activity_id", activityId);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        final String requestStr = jsonObject.toString();
        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), requestStr);
        activityInfoCall = retrofitSerives.getActivityInfo(body);
        activityInfoCall.enqueue(new Callback<ActivityInfo>() {
            @Override
            public void onResponse(Call<ActivityInfo> call, Response<ActivityInfo> response) {
                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0")) {
                        if (response.body().getResult() != null) {
                            view.setActivityInfo(response.body());
                        }else {
                            view.onError(Constants.ACTIVITYINFO_NULL);
                        }
                    } else {
                        view.onError(Constants.ACTIVITYINFO_NULL);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<ActivityInfo> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });


    }

    @Override
    public void stopRequest() {

    }
}
